'use strict';

let fs = require('fs');
let { sequelize } = require('../db');

// 搜索所有的模型定义文件
function searchFiles() {
    let files = fs.readdirSync(__dirname);
    return files.filter(name => {
        return name.endsWith('.js') && name !== 'index.js';
    })
}

// 注册（引入）所有的模型
function registerModels(files) {
    let obj = {};
    files.forEach(name => {
        let modelName = name.substring(0, name.length - 3);
        obj[modelName] = require(__dirname + '/' + name);
    })

    return obj;
}


let files = searchFiles();
let obj = registerModels(files);

obj.sync = async () => {
    return sequelize.sync({ force: true })
}

module.exports = obj;